package com.qersoft.controller;

import com.qersoft.entity.ClassEntity;
import com.qersoft.entity.ResponseMessage;
import com.qersoft.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    /**
     * 创建班级
     *
     * @param classEntity 班级实体
     * @return 创建的班级
     */
    @PostMapping("/create")
    public String createClass(@RequestBody ClassEntity classEntity) {
        if (classService.createClass(classEntity)) {
            return "创建班级成功";
        }else {
            return "创建班级失败";
        }
    }

    /**
     * 获取班级信息
     * @param classId 班级ID
     * @return 班级实体
     */
    @GetMapping("/{classId}")
    public ResponseMessage<ClassEntity> getClassById(@PathVariable Long classId) {
        ClassEntity classEntity = classService.getClassById(classId);
        if (classEntity != null) {
            return ResponseMessage.message(classEntity);
        } else {
            return ResponseMessage.message(null); // 返回null，表示班级不存在
        }
    }

    /**
     * 更新班级信息
     * @param classEntity 班级实体
     * @return 更新结果
     */
    @PutMapping("/update")
    public String updateClass(@RequestBody ClassEntity classEntity) {
        boolean result = classService.updateClass(classEntity);
        if (result) {
            return "更新班级成功";
        }else {
            return "更新班级失败";
        }
    }

    /**
     * 删除班级
     * @param classId 班级ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{classId}")
    public String deleteClass(@PathVariable Long classId) {
        boolean result = classService.deleteClass(classId);
        if (result) {
            return "删除班级成功";
        }else{
            return "删除班级失败";
        }
    }

    /**
     * 获取所有班级
     * @return 班级列表
     */
    @GetMapping("/all")
    public ResponseMessage<List<ClassEntity>> getAllClasses() {
        List<ClassEntity> classes = classService.getAllClasses();
        return ResponseMessage.message(classes);
    }
}
